/**
 * Created by cherryzou on 2017/6/25.
 */
var $=require('jquery');

const Req={

        proUrl:'http://172.16.1.89:8285/',
        //proUrl:"http://192.168.2.188:8081/yxt-web",
        request(opts){
           var XHR=null,arr=[],postData='';
            opts=opts || {};
            opts.methods=opts.methods.toUpperCase() || 'POST';

            opts.success=opts.success || function(){}
            opts.error=opts.error || function(){}
            opts.data=opts.data || null;
            opts.aynsc=opts.aynsc||true;
            XHR=XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
            for(var k in opts.data){
                arr.push(k+"="+opts.data[k]);
            }
            postData=arr.join("&");

            XHR.onreadystatechange=function(){
              if(XHR.readyState==4 && XHR.status==200){
                console.log(XHR.responseText)
                opts.success &&  opts.success(XHR.responseText)
              }else{
                opts.error(XHR.responseText)
              }
            }

            switch (opts.methods.toUpperCase()){
                case "POST":
                    XHR.open(opts.methods,opts.url,opts.async);
                    XHR.send(opts.data);
                break;
                case "GET":
                    XHR.open(opts.methods,opts.url+'?'+postData,opts.async);
                    XHR.send(null);
                break;
               case "PUT":
                 XHR.open(opts.methods,opts.url,opts.async);
                 XHR.send(opts.data);
                 XHR.setRequestHeader( "X-HTTP-Method-Override","PUT");
                break;
              case "DELETE":

                XHR.open(opts.methods, url, true);
                XHR.send(null);
              break;
            }



        },
       _ajax(opt){
            var _this=this;
            this.request({
                methods:opt.methods||'POST',
                url:_this.proUrl+opt.url,
                data:opt.data,
                success:opt.success,
                error:opt.error
            })
       },
    jQ_ajax(opt){
           var _this=this;
           $.ajax({

               url:_this.proUrl+opt.url,
               success:function(data){
                   opt.success && opt.success(data);

               },
               error: function(data){
                   opt.error && opt.error(data);
               },
               async: opt.async || true,
               dataType: 'json',
               data:opt.data,
               type: opt.type,
               timeout: 60000
           })

    },
    promise(){


    }


}

export default Req;
